﻿Public Class frmGiaoVien

    Private Sub frmGiaoVien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LayDSGV()
        If (txtMaGV.Text.Trim().Length > 0) Then
            cbXoaNS.Checked = False
            dtNgaySinh.Enabled = True
        End If
    End Sub

    Private Sub LayDSGV()
        Try
            Dim gvBUS As New GiaoVienBUS
            Dim ds As New List(Of GiaoVienDTO)
            Dim i As Integer = 1
            Dim ns As String = Nothing

            ds = gvBUS.LayDanhSachGiaoVien()
            dgGiaoVien.Rows.Clear()

            For Each gv As GiaoVienDTO In ds
                If (gv.NgaySinh <> Date.Parse("1/1/1900")) Then
                    ns = gv.NgaySinh
                Else
                    ns = ""
                End If
                dgGiaoVien.Rows.Add(i, gv.MaGV, gv.TenGV, gv.GioiTinh, ns, gv.SoDienThoai, gv.Email, gv.DiaChi)
                i += 1
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btThem.Click
        Try
            If (txtMaGV.Text.Trim.Length = 0) Then
                MessageBox.Show("Nhap Ma Giao Vien.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            ElseIf (txtTenGV.Text.Trim.Length = 0) Then
                MessageBox.Show("Nhap Ten Giao Vien.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            End If

            Dim gvDTO As New GiaoVienDTO()
            Dim gvBUS As New GiaoVienBUS()
            Dim kq As Integer = -1

            gvDTO.MaGV = txtMaGV.Text.Trim()
            gvDTO.TenGV = txtTenGV.Text.Trim()
            If (rdNam.Checked = True) Then
                gvDTO.GioiTinh = "Nam"
            ElseIf (rdNu.Checked = True) Then
                gvDTO.GioiTinh = "Nu"
            End If

            If (cbXoaNS.Checked) Then
                gvDTO.NgaySinh = Date.Parse("1/1/1900")
            Else
                gvDTO.NgaySinh = dtNgaySinh.Value
            End If

            gvDTO.SoDienThoai = txtSoDT.Text.Trim()
            gvDTO.DiaChi = txtDiaChi.Text.Trim()
            gvDTO.Email = txtEmail.Text.Trim()

            kq = gvBUS.ThemGiaoVien(gvDTO)

            If kq > 0 Then
                LayDSGV()
                MessageBox.Show("Them thanh cong.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("Thêm that bai.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If

        Catch ex As Exception
            If (ex.Message.IndexOf("create duplicate values in the index, primary key, or relationship") > 0) Then
                MessageBox.Show("Ma GV da ton tai.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End Try
    End Sub

    Private Sub btXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btXoa.Click
        Try
            If (txtMaGV.Text.Trim.Length = 0) Then
                MessageBox.Show("Nhap Ma Giao Vien muon Xoa.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            End If

            Dim gvDTO As New GiaoVienDTO()
            Dim gvBUS As New GiaoVienBUS()
            Dim kq As Integer = -1

            gvDTO.MaGV = txtMaGV.Text.Trim()
            kq = gvBUS.XoaGiaoVien(gvDTO)

            If kq > 0 Then
                LayDSGV()
                MessageBox.Show("Xoa thanh cong.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("Xoa that bai.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCapNhat.Click
        Try
            If (txtMaGV.Text.Trim.Length = 0) Then
                MessageBox.Show("Nhap Ma Giao Vien.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            ElseIf (txtTenGV.Text.Trim.Length = 0) Then
                MessageBox.Show("Nhap Ten Giao Vien.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            End If

            Dim gvDTO As New GiaoVienDTO()
            Dim gvBUS As New GiaoVienBUS()
            Dim kq As Integer = -1

            gvDTO.MaGV = txtMaGV.Text.Trim()
            gvDTO.TenGV = txtTenGV.Text.Trim()
            If (rdNam.Checked = True) Then
                gvDTO.GioiTinh = "Nam"
            ElseIf (rdNu.Checked = True) Then
                gvDTO.GioiTinh = "Nu"
            End If

            gvDTO.NgaySinh = dtNgaySinh.Value
            gvDTO.SoDienThoai = txtSoDT.Text.Trim()
            gvDTO.DiaChi = txtDiaChi.Text.Trim()
            gvDTO.Email = txtEmail.Text.Trim()

            kq = gvBUS.SuaGiaoVien(gvDTO)

            If kq > 0 Then
                LayDSGV()
                MessageBox.Show("Cap Nhat thanh cong.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("Cap Nhat that bai.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub dgGiaoVien_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgGiaoVien.SelectionChanged
        Try
            If (dgGiaoVien.SelectedRows.Count > 0) Then
                If (dgGiaoVien.SelectedRows(0).Cells(1).Value = Nothing) Then
                    Return
                End If

                txtMaGV.Text = dgGiaoVien.SelectedRows(0).Cells(1).Value.ToString().Trim()
                txtTenGV.Text = dgGiaoVien.SelectedRows(0).Cells(2).Value.ToString().Trim()

                If (dgGiaoVien.SelectedRows(0).Cells(3).Value.ToString().Trim() = "Nam") Then
                    rdNam.Checked = True
                ElseIf (dgGiaoVien.SelectedRows(0).Cells(3).Value.ToString().Trim() = "Nu") Then
                    rdNu.Checked = True
                End If

                If (dgGiaoVien.SelectedRows(0).Cells(4).Value <> Nothing) Then
                    dtNgaySinh.Value = dgGiaoVien.SelectedRows(0).Cells(4).Value
                End If

                txtSoDT.Text = dgGiaoVien.SelectedRows(0).Cells(5).Value.ToString().Trim()
                txtEmail.Text = dgGiaoVien.SelectedRows(0).Cells(6).Value.ToString().Trim()
                txtDiaChi.Text = dgGiaoVien.SelectedRows(0).Cells(7).Value.ToString().Trim()
            End If

            If (txtMaGV.Text.Trim().Length > 0) Then
                cbXoaNS.Checked = False
                dtNgaySinh.Enabled = True
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btThoat.Click
        Me.Close()
    End Sub

    Private Sub btTim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btTim.Click
        Try
            Dim gvDTO As New GiaoVienDTO()
            Dim gvBUS As New GiaoVienBUS()
            Dim kq As Integer = -1

            gvDTO.MaGV = txtMaGV.Text.Trim()
            gvDTO.TenGV = txtTenGV.Text.Trim()
            If (rdNam.Checked = True) Then
                gvDTO.GioiTinh = "Nam"
            ElseIf (rdNu.Checked = True) Then
                gvDTO.GioiTinh = "Nu"
            End If

            If (cbXoaNS.Checked) Then
                gvDTO.NgaySinh = Nothing
            Else
                gvDTO.NgaySinh = dtNgaySinh.Value.ToString().Split(" ")(0)
            End If

            gvDTO.SoDienThoai = txtSoDT.Text.Trim()
            gvDTO.DiaChi = txtDiaChi.Text.Trim()
            gvDTO.Email = txtEmail.Text.Trim()

            Dim ds As New List(Of GiaoVienDTO)
            Dim i As Integer = 1
            Dim ns As String = ""

            ds = gvBUS.TraCuuGiaoVien(gvDTO)
            dgGiaoVien.Rows.Clear()

            For Each gv As GiaoVienDTO In ds
                If (gv.NgaySinh <> Date.Parse("1/1/1900")) Then
                    ns = gv.NgaySinh
                Else
                    ns = ""
                End If
                dgGiaoVien.Rows.Add(i, gv.MaGV, gv.TenGV, gv.GioiTinh, ns, gv.SoDienThoai, gv.Email, gv.DiaChi)
                i += 1
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub cbXoaNS_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbXoaNS.CheckedChanged
        If (cbXoaNS.Checked) Then
            dtNgaySinh.Enabled = False
        Else
            dtNgaySinh.Enabled = True
        End If
    End Sub

    Private Sub btXoaTT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btXoaTT.Click
        txtMaGV.Text = ""
        txtDiaChi.Text = ""
        txtEmail.Text = ""
        txtSoDT.Text = ""
        txtTenGV.Text = ""
        rdNam.Checked = False
        rdNu.Checked = False
        cbXoaNS.Checked = True
        dtNgaySinh.Enabled = False
    End Sub
End Class